package models;

import java.sql.*;

public class AccountManagement {
	// Database Instance
	private final String DRIVER = "org.postgresql.Driver";
	private final String URL = "jdbc:postgresql://localhost:5432/CourseRegistrationSystem";
	private final String SQL_USER = "postgres";
	private final String SQL_PASS = "dkmh";
	// Establish Connect
	private Connection myConnection = null;
	private PreparedStatement stm = null;
	private ResultSet rs = null;
	private String userId, userName, department, classId, email, passw, role;

	public AccountManagement(String userId) throws ClassNotFoundException,
			SQLException {
		this.userId = userId;
		Class.forName(DRIVER);
		myConnection = DriverManager.getConnection(URL, SQL_USER, SQL_PASS);
		String query = "SELECT * FROM sinhvien WHERE masv=" + "'" + userId
				+ "'";
		stm = myConnection.prepareStatement(query);
		rs = stm.executeQuery();
		while (rs.next()) {
			userId = rs.getString("masv");
			userName = rs.getString("tensv");
			department = rs.getString("khoa");
			classId = rs.getString("lop");
			email = rs.getString("emailsv");
			passw = rs.getString("passw");
			role = rs.getString("role");
		}
	}

	public String getId() {
		return userId;
	}

	public String getName() {
		return userName;
	}

	public String getClassId() {
		return classId;
	}

	public String getDepartment() {
		return department;
	}

	public String getEmail() {
		return email;
	}

	public String getPass() {
		return passw;
	}

	public String getRole() {
		return role;
	}

	public boolean checkUser(String userId, String passw) {
		if (userId.equals(this.userId) && passw.equals(this.passw))
			return true;
		return false;
	}

	public void updatePassword(String newPass) {
		String query = "UPDATE sinhvien SET passw = '" + newPass
				+ "' WHERE masv = '" + userId + "'";
		try {
			stm = myConnection.prepareStatement(query);
			stm.execute();
			System.out.println("Done");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		AccountManagement am = new AccountManagement("12130355");
		System.out.println(am.getRole());
	}

}
